package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsFailedLibraryMapper;
import com.jy.wms.dao.WmsStockMapper;
import com.jy.wms.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsFailedLibraryService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Tue Jan 23 09:32:56 GMT+08:00 2018
 */

@Service
public class WmsFailedLibraryService extends BaseService<WmsFailedLibraryMapper, WmsFailedLibrary> {
    @Autowired
    private WmsShelvesService wmsShelvesService;
    @Autowired
    private WmsStockMapper wmsStockMapper;

    /**
     * 分页查询  当前仓库，当前货主下的质检状态为   “4”（质检完成） 的预期到货通知单（ASN）
     *
     * @param entity 实体
     * @return Page<WmsFailedLibrary>
     */
    @BaseServiceDescribe(moduleName = "不合格品入库", btnName = "分页查询")
    public Page<WmsEstimatedArrivalOrder> baseSelectPage(WmsEstimatedArrivalOrder entity, String hzid, String ssckid, String ckmc, String hzmc) throws Exception {
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsEstimatedArrivalOrder>) dao.baseSelectPage(entity, hzid, ssckid, ckmc, hzmc);
        } catch (Exception e) {
            throw new ServiceException("查询预期到货通知单列表", e);
        }
    }

    /**
     * 根据预期到货明细查询收货表里面质检状态为“不合格”的记录
     *
     * @param asnId 预期到货通知单id
     * @return
     */
    public Page<WmsShelves> getReceiveList(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            //根据预期到货通知单id查找明细id集合
            //List<String> idList = dao.getIdList(asnId);
            PageHelper.startPage(page, rows);
            return (Page<WmsShelves>) dao.getReceiveList(asnId);
        } catch (Exception e) {
            throw new ServiceException("查询预期到货通知单列表", e);
        }

    }

    /**
     * 库位模糊查询 管控的数据
     *
     * @param show_kwid
     * @return
     */
    public List<WmsLibraryPosition> searchKw(String show_kwid, String ckid, String hzid) throws ServiceException {
        try {

            return dao.searchKw(show_kwid, ckid, hzid);
        } catch (Exception e) {
            throw new ServiceException("库位模糊查询失败", e);
        }
    }

    /**
     * 出库
     *
     * @param list
     * @param ckid
     * @param username
     * @throws Exception
     */
    @Transactional
    public void add(List<WmsShelves> list, String username, String ckid) throws Exception {
        int a =0;
        int b =0;
        int count =0;
        boolean flag=true;
        for(WmsShelves wmsShelves:list){
            WmsStock wmsStock=wmsStockMapper.selectById(wmsShelves.getKcid());//库存
            if(Integer.parseInt(wmsStock.getSl())< Integer.parseInt(wmsShelves.getSjsl())){
                flag=false;
            }
            //如果待下架数量，待转移数量，冻结数量，破损数量，补货数量其中一个大于0时提示操作错误
            if(Double.parseDouble(wmsStock.getDxjsl())>0|| Double.parseDouble(wmsStock.getDzysl())>0|| Double.parseDouble(wmsStock.getDjsl())>0|| Double.parseDouble(wmsStock.getPssl())>0|| Double.parseDouble(wmsStock.getBhsl())>0){
                flag=false;
                break;
            }
        }
        if(flag==true){
            for(WmsShelves wmsShelves:list){
                WmsFailedLibrary wmsFailedLibrary = new WmsFailedLibrary();
                WmsStock wmsStock=wmsStockMapper.selectById(wmsShelves.getKcid());//库存
                Integer sl= Integer.parseInt(wmsStock.getSl())- Integer.parseInt(wmsShelves.getSjsl());
                //修改库存
                a = dao.updateStock(wmsShelves.getKcid(), String.valueOf(sl));
                wmsFailedLibrary.setId(UUIDUtils.uuid());
                wmsFailedLibrary.setAsnId(wmsShelves.getYqdhbdid());
                wmsFailedLibrary.setAsnMxId(wmsShelves.getYqdhbdmxid());
                wmsFailedLibrary.setHpbh(wmsShelves.getShow_bh());
                wmsFailedLibrary.setHpsl(new BigDecimal(wmsShelves.getSjsl()));
                wmsFailedLibrary.setKcid(wmsShelves.getKcid());
               // wmsFailedLibrary.setKwid(wmsShelves.getKwid());
                wmsFailedLibrary.setCkid(ckid);
                wmsFailedLibrary.setShid(wmsShelves.getSsshid());
                wmsFailedLibrary.setCzr(username);
                //增加出库记录
                b=dao.baseInsert(wmsFailedLibrary);
                count++;

            }
            if (count > 0) {
                throw new SucessException("确认出库成功");
            } else {
                throw new ServiceException("确认出库失败");
            }
        }else{
            throw new ServiceException("确认失败，库存异常，请联系管理员");
        }
    }


    /**
     * 通过asn id 查询质检单明细
     *
     * @param asnId
     * @return
     */
    public Page<WmsQualityInspectionListDetailed> getQualityDetail(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            PageHelper.startPage(page, rows);
            return (Page<WmsQualityInspectionListDetailed>) dao.getQualityDetail(asnId);
        } catch (Exception e) {
            throw new ServiceException("质检单明细查询", e);
        }
    }


    /**
     * 质检明细附件
     *
     * @return
     */
    public Page<WmsQcEnclosure> getQcEnclosure(String asnId, Integer page, Integer rows) throws ServiceException {
        try {
            PageHelper.startPage(page, rows);
            return (Page<WmsQcEnclosure>) dao.getQcEnclosure(asnId);
        } catch (Exception e) {
            throw new ServiceException("质检单附件查询", e);
        }
    }


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(WmsFailedLibrary entity) {
        return dao.baseInsert(entity);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsFailedLibrary entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }


    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsFailedLibrary
     */
    public WmsFailedLibrary baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsFailedLibrary> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsFailedLibrary entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsFailedLibrary entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }


    /**
     * 导出不合格品处理入库信息
     *
     * @param ids
     * @return
     */
    @BaseServiceDescribe(moduleName = "导出不合baseSelectPage格品处理入库信息", btnName = "导出不合格品处理入库信息")
    public List<WmsShelves> getReceiveListOutPut(List<String> ids, Map map) {

        List<WmsShelves> list = dao.getReceiveListOutPut(ids,map);
        return list;

    }

}
